﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories.UserRepositories;
using Icoders.MarketTracker.Core.Validation;
using Icoders.MarketTracker.Data.EF;
using Icoders.MarketTracker.Data.Utils;

namespace Icoders.MarketTracker.Data.Repository.MasterDataRepositories.UserRepositories
{
    public class SysRoleRepository : RepositoryBase, ISysRoleRepository
    {
       private AppDataContext _ctx;

       public SysRoleRepository(AppDataContext ctx)
       {
           _ctx = ctx;
       }
        public ValidationResultInfo Validate(SysRole objToValidate)
        {

            ValidationResultInfo vri = objToValidate.BasicValidation();
            bool hasDuplicateName = GetAll().Where(v => v.Id != objToValidate.Id)
                .Any(p => p.Name == objToValidate.Name);
            if (hasDuplicateName)
                vri.Results.Add(new ValidationResult("Duplicate Name Found"));
            return vri;
        }

        public int Save(SysRole entity)
        {
            ValidationResultInfo vri = Validate(entity);
            if (!vri.IsValid)
            {
                throw new DomainValidationException(vri, "Role Details provided not Valid");
            }
            DateTime date = TimeUtil.GetDateTime();
            TblRole tbl = _ctx.TblRole.FirstOrDefault(s => s.Id == entity.Id);
            if (tbl == null)
            {
                tbl = new TblRole();
                tbl.CreatedOn = date;
                tbl.StatusId = (int)MasterEntityStatus.New;
                _ctx.TblRole.AddObject(tbl);
                
                tbl.Description = entity.Description;
            }
            tbl.UpdatedOn = date; ;
            tbl.Name = entity.Name;
            tbl.Description = entity.Description;

            _ctx.SaveChanges();
            return tbl.Id;
        }

        public SysRole GetById(int id)
        {
            var tbl = _ctx.TblRole.FirstOrDefault(s => s.Id == id);
            if (tbl == null) return null;
            return Map(tbl);
        }

       private SysRole Map(TblRole tbl)
       {
           return new SysRole
                      {
                          Id = tbl.Id,

                          Name = tbl.Name,

                          Description = tbl.Description,
                          UpdatedOn = tbl.UpdatedOn,
                          CreatedOn = tbl.CreatedOn,
                          Status = (MasterEntityStatus) tbl.StatusId
                      };
       }

       public List<SysRole> GetAll()
        {
            return _ctx.TblRole.ToList().Select(n => Map(n)).ToList();
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        protected override string _cacheKey
        {
            get { throw new NotImplementedException(); }
        }

        protected override string _cacheListKey
        {
            get { throw new NotImplementedException(); }
        }
    }
}
